package com.qiantu.phone.widget.setting;

import static android.util.TypedValue.COMPLEX_UNIT_PX;

import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.QueryCodeResultBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.WheelValueBean;
import com.qiantu.phone.event.DeviceTypeChangeEvent;
import com.qiantu.phone.ui.dialog.BottomWheelViewDialog;
import com.lxj.xpopup.XPopup;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class DeviceSettingCalibration extends SettingBar implements View.OnClickListener{
    private DeviceBean mDevice;
    private AppActivity mContext;
    private WheelValueBean selectedValue;
    private double mCalibration;

    public DeviceSettingCalibration(AppActivity context, DeviceBean deviceBean) {
        this(context, null, deviceBean);
    }

    public DeviceSettingCalibration(AppActivity context, AttributeSet attrs, DeviceBean deviceBean) {
        this(context, attrs, 0, deviceBean);
    }

    public DeviceSettingCalibration(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean deviceBean) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style, deviceBean);
    }

    public DeviceSettingCalibration(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, DeviceBean deviceBean) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDevice = deviceBean;
        mContext = context;
        setLeftText(R.string.device_setting_calibration);
        mCalibration = deviceBean.getStateData().getSettings().getCalibrationvalue();
        setRightTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor2));
        setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setRightTextSize(COMPLEX_UNIT_PX, DensityUtils.dip2px(context, 12));
        setOnClickListener(this::onClick);
    }

    private BottomWheelViewDialog dialog;
    private void showDialog(){
        if (dialog == null){
            dialog = new BottomWheelViewDialog(mContext, getData());
            dialog.setLabel(getResources().getString(R.string.device_setting_calibration));
            dialog.setDialogClickListener(new BottomWheelViewDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(WheelValueBean wheelValue) {
                    dialog.dismiss();
                    selectedValue = wheelValue;
                    changeType();
                }
            });
            new XPopup.Builder(getContext())
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(dialog);
        }
        dialog.setCurItem(mCalibration + "");
        dialog.show();
    }

    private List<WheelValueBean> getData(){
        List<WheelValueBean> items = new ArrayList<>();
        for (int i=0; i<41; i++){
            double value;
            if(mDevice.getDeviceType().equals("temperature_sensor")){
                value = -10 + 0.5 * i;
            }else{
                value = -20 + i;
            }
            WheelValueBean bean = new WheelValueBean(value + "", value + "");
            items.add(bean);
        }
        return items;
    }


    private void changeType() {
        Map<String, Object> stateData = new HashMap<>();
        Map<String, Object> settings = new HashMap<>();
        settings.put("calibrationvalue", selectedValue.getValue());
        stateData.put("settings", settings);
        LLHttpManager.sendDeviceSetting(mContext, mDevice.getDeviceSerialNo(), stateData, new HttpCallback<HttpData<QueryCodeResultBean<String, Object>>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity){
                    ((AppActivity)getContext()).showDialog(getResources().getString(R.string.setting));
                }
            }

            @Override
            public void onSucceed(HttpData<QueryCodeResultBean<String, Object>> result) {
                super.onSucceed(result);
                hideDialog();
                mDevice.getStateData().getSettings().setCalibrationvalue(Double.parseDouble(selectedValue.getValue()));
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
//                setRightText(selectedValue.getLabel());
                EventBus.getDefault().post(new DeviceTypeChangeEvent(mDevice.getDeviceSerialNo()));
                ToastUtils.show(R.string.set_success);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {

            }
        });
    }



    private void hideDialog(){
        if(getContext() != null && getContext() instanceof AppActivity){
            ((AppActivity)getContext()).hideDialog();
        }
    }

    @Override
    public void onClick(View v) {
        showDialog();
    }
}
